package com.jy.app.lib.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;

/**
 * 简单对fastjson的使用封装
 * 关于pojo的一些字段的特殊处理，请看https://github.com/alibaba/fastjson/wiki/JSONField
 * Created by jan on 2016/4/13.
 */
public class JsonUtil {
    private static final String TAG = "JsonUtil";

    public static String toJson(Object object) {
        String json = JSON.toJSONString(object);
        return json;
    }


    public static String toJsonIgnoreNull(Object object) {
        return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue);
    }


    public static <T> T parseObject(String json,Class<T> cls) {
        T t = null;
        if (!TextUtils.isEmpty(json)) {
             t = JSON.parseObject(json,cls);
        }
        return t;
    }

    public static <T> T parseObject(String json,TypeReference<T> type) throws Exception {
        T t = null;
        if (!TextUtils.isEmpty(json)) {
            t = JSON.parseObject(json, type);
        }
        return t;
    }

    public  static  <T> List<T> parseList(String jsonArray,Class<T> clazz){
        if(!TextUtils.isEmpty(jsonArray)){
            return JSON.parseArray(jsonArray,clazz);
        }
        return null;
    }
}
